/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.session;

import com.thienloc.omap.entity.SyntaxDefinition;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@Stateless
public class SyntaxDefinitionFacade extends AbstractFacade<SyntaxDefinition> {

    private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
    @PersistenceContext(unitName = "omapPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SyntaxDefinitionFacade() {
        super(SyntaxDefinition.class);
    }

    public List<SyntaxDefinition> findBySyntax(String syntax) throws Exception {
        try {
            TypedQuery<SyntaxDefinition> q = em.createNamedQuery("SyntaxDefinition.findBySyntax", SyntaxDefinition.class);
            q.setParameter("syntax", syntax);
            return q.getResultList();
        } catch (NoResultException ex) {
            return new ArrayList<SyntaxDefinition>(0);
        } catch (Exception ex) {
            logger.error("", ex);
            return new ArrayList<SyntaxDefinition>(0);
        }
    }

    public List<SyntaxDefinition> findByUsername(String username) throws Exception {
        try {
            TypedQuery<SyntaxDefinition> q = em.createNamedQuery("SyntaxDefinition.findByUsername", SyntaxDefinition.class);
            q.setParameter("username", username);
            return q.getResultList();
        } catch (NoResultException ex) {
            return new ArrayList<SyntaxDefinition>(0);
        } catch (Exception ex) {
            logger.error("", ex);
            return new ArrayList<SyntaxDefinition>(0);
        }
    }

    public List<String> findSyntaxesByUsername(String username) throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("SyntaxDefinition.findSyntaxesByUsername", String.class);
            q.setParameter("username", username);
            List<String> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            logger.error("", ex);
            return null;
        }
    }

    public List<String> findAllSyntax() throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("SyntaxDefinition.findAllSyntax", String.class);
            List<String> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            logger.error("", ex);
            return null;
        }
    }

    public List<String> findAllUsername() throws Exception {
        try {
            TypedQuery<String> q = em.createNamedQuery("SyntaxDefinition.findAllUsername", String.class);
            List<String> result = q.getResultList();
            return result;
        } catch (NoResultException ex) {
            return null;
        } catch (Exception ex) {
            logger.error("", ex);
            return null;
        }
    }
}